from django.views.decorators.csrf import csrf_exempt
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import JsonResponse
import json
from LogisticsCard.models import Menu
# 菜单权限快速修改
@csrf_exempt
@login_required
def menu_update_permission_view(request, menu_id):
    if request.method == 'POST':
        menu = get_object_or_404(Menu, id=menu_id, delete_at__isnull=True)
        permission = int(request.POST.get('permission', 0))
        menu.permission = permission
        menu.save()
    return redirect('/menu/')

# 菜单状态快速修改
@csrf_exempt
@login_required
def menu_update_status_view(request, menu_id):
    if request.method == 'POST':
        menu = get_object_or_404(Menu, id=menu_id, delete_at__isnull=True)
        menu.status = not menu.status
        menu.save()
    return redirect('/menu/')

@login_required
def menu_list_view(request):
    menus = Menu.objects.filter(is_active=True, delete_at__isnull=True).order_by('order')
    return render(request, 'menu/index.html', {'menus': menus})

@login_required
def menu_create_view(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        icon = request.POST.get('icon')
        url = request.POST.get('url')
        order = request.POST.get('order') or 0
        parent_id = request.POST.get('parent')
        remark = request.POST.get('remark')
        permission = int(request.POST.get('permission', 0))
        status = bool(int(request.POST.get('status', 1)))
        parent = Menu.objects.filter(id=parent_id).first() if parent_id else None
        Menu.objects.create(name=name, icon=icon, url=url, order=order, parent=parent, remark=remark, permission=permission, status=status, delete_at=None)
        return redirect('/menu/')
    parents = Menu.objects.filter(parent=None)
    return render(request, 'menu/create.html', {'parents': parents})

@login_required
def menu_edit_view(request, menu_id):
    menu = get_object_or_404(Menu, id=menu_id, delete_at__isnull=True)
    if request.method == 'POST':
        menu.name = request.POST.get('name')
        menu.icon = request.POST.get('icon')
        menu.url = request.POST.get('url')
        menu.order = request.POST.get('order') or 0
        parent_id = request.POST.get('parent')
        menu.parent = Menu.objects.filter(id=parent_id).first() if parent_id else None
        menu.remark = request.POST.get('remark')
        menu.permission = int(request.POST.get('permission', 0))
        menu.status = bool(int(request.POST.get('status', 1)))
        menu.save()
        return redirect('/menu/')
    parents = Menu.objects.filter(parent=None).exclude(id=menu.id)
    return render(request, 'menu/edit.html', {'menu': menu, 'parents': parents})

@login_required
def menu_delete_view(request, menu_id):
    from django.utils import timezone
    menu = get_object_or_404(Menu, id=menu_id, delete_at__isnull=True)
    menu.delete_at = timezone.now()
    menu.save()
    return redirect('/menu/')

@csrf_exempt
@login_required
def menu_update_order_view(request):
    """处理菜单拖拽排序"""
    if request.method == 'POST':
        try:
            data = json.loads(request.body)
            menu_orders = data.get('menu_orders', [])
            
            for item in menu_orders:
                menu_id = item.get('id')
                new_order = item.get('order')
                menu = Menu.objects.filter(id=menu_id, delete_at__isnull=True).first()
                if menu:
                    menu.order = new_order
                    menu.save()
            
            return JsonResponse({'success': True})
        except Exception as e:
            return JsonResponse({'success': False, 'error': str(e)})
    
    return JsonResponse({'success': False, 'error': 'Invalid request method'})
